package com.hrserviceserve.service;

import com.hrserviceserve.dao.ContractRepository;
import com.hrserviceserve.entity.Contract;
import com.hrserviceserve.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ContractService {//合同
    @Autowired
    ContractRepository contractRepository;

    public Result findAll(){
        List<Contract> all = contractRepository.findAll();
        return Result.success(all);
    }
    public Result findByPage(Map<String,Object> map){
        PageRequest pageRequest=PageRequest.of((int)map.get("page")-1,(int)map.get("limit"));
        Page<Contract> page=contractRepository.findAllWhere((String) map.get("name"),(String) map.get("position"),pageRequest);
        return Result.success(page);
    }
    public Result save(Contract contract){
        Contract save = contractRepository.save(contract);
        if (save!=null){
            return Result.success(save);
        }else {
            return Result.fail();
        }
    }
    @Transactional
    public int delete(Integer id){
        return contractRepository.deleteByContractId(id);
    }
}
